//
//  ViewController.m
//  WXMediaEngineDemo
//
//  Created by tianlong on 2018/6/22.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "ViewController.h"
#import "WXMediaEngine.h"

@interface ViewController ()<WXMediaEngineDelegate>
@property (nonatomic, strong) WXMediaEngine *mediaEngine;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
     @"http://download.lingyongqian.cn/music/AdagioSostenuto.mp3",
     @"http://download.lingyongqian.cn/music/ForElise.mp3",
     @"http://mr7.doubanio.com/39ec9c9b5bbac0af7b373d1c62c294a3/1/fm/song/p1393354_128k.mp4",
     @"http://mr7.doubanio.com/16c59061a6a82bbb92bdd21e626db152/0/fm/song/p966452_128k.mp4"
     http://download.lingyongqian.cn/music/AdagioSostenuto.mp3
     https://o79mx21qy.qnssl.com/lmr-cTeyUxoZqNX8nH4_174k_7Px
     */
    
    self.mediaEngine = [[WXMediaEngine alloc] initWithURL:@"https://o79mx21qy.qnssl.com/lmr-cTeyUxoZqNX8nH4_174k_7Px"];
    [self.slider addTarget:self action:@selector(sliderDragging:) forControlEvents:UIControlEventValueChanged];
    [self.slider addTarget:self action:@selector(sliderBeginDrag:) forControlEvents:UIControlEventTouchDown];
    [self.slider addTarget:self action:@selector(sliderEndDrag:) forControlEvents:UIControlEventTouchUpInside];
    self.mediaEngine.delegate = self;
}

- (IBAction)play:(id)sender {
    [self.mediaEngine start];
}

- (IBAction)pause:(id)sender {
    [self.mediaEngine pause];
}

- (IBAction)stop:(id)sender {
    [self.mediaEngine stop];
    [self.slider setValue:0];
}

- (void)sliderBeginDrag:(UISlider *)slider {
    [self.mediaEngine pause];
}

- (void)sliderDragging:(UISlider *)slider {
}

- (void)sliderEndDrag:(UISlider *)slider {
//    NSString *value = [NSString stringWithFormat:@"%.2f",slider.value];
//    long long fromOffset = self.mediaEngine.fileLength *value.floatValue;
//    NSLog(@"sliderEndDrag = %.2f fileLength = %lld fromOffset = %lld",value.floatValue,self.mediaEngine.fileLength,fromOffset);
    NSLog(@"slider_value = %f",slider.value);
}


- (void)mediaEngine:(WXMediaEngine *)mediaEngine downloadProgress:(CGFloat)progress {
    [self.slider  setValue:progress];
}


@end
